package Dsvanbangopy.backing;

import Dsvanbangopy.Dsvanbangopy;

import Dsvanbangopy.resource.DsvanbangopyBundle;

import common.Utils;

import javax.faces.context.FacesContext;

import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.WindowState;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.uicli.binding.JUCtrlRangeBinding;

public class DsvanbangopyBacking {
    private String langid;
    private String mPortletTitle;
    private String pageChitietvanban;
    private String tieudedsvbgopy;
    private int pageNumber;
    private boolean paging = false;
    private static final int RANGE_SIZE = 10;
    private String viewRender;
    private String labelTrangdau;
    private String labelTrangcuoi;
    private String labelTrangtruoc;
    private String labelTrangsau;
    private String labelNoData;
    private String labelAccess;

    public String getViewRender() {
        checkPaging();
        return viewRender;
    }

    private void checkPaging() {
        Utils.iteratorRefresh("DtvbDsVanbanGopyView1Iterator");
        long lastPageNum =
            Math.max(1, (getDsVanbanView().getEstimatedRowCount() - 1) /
                     RANGE_SIZE + 1);
        if (lastPageNum > 1) {
            setPaging(true);
        }
    }

    public String getPortletTitle() {
        if (mPortletTitle == null) {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            PortletPreferences preferences = request.getPreferences();
            mPortletTitle =
                    preferences.getValue(Dsvanbangopy.PORTLETTITLE_KEY, getResource(DsvanbangopyBundle.PORTLETTITLE));
        }
        return mPortletTitle;
    }

    public String getPortletTitleLabel() {
        String resource = getResource(DsvanbangopyBundle.PORTLETTITLE);
        return resource;
    }
    
    public void setPortletTitle(String portletTitle) {
        mPortletTitle = portletTitle;
    }

    public String applyAction() throws Exception {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletPreferences preferences = request.getPreferences();
        preferences.setValues(Dsvanbangopy.PORTLETTITLE_KEY,
                              mPortletTitle.split(","));
        preferences.store();
        return null;
    }

    public String getApplyLabel() {
        String resource = getResource(DsvanbangopyBundle.APPLY_LABEL);
        return resource;
    }

    public String okAction() throws Exception {
        applyAction();
        ActionResponse response =
            (ActionResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setPortletMode(PortletMode.VIEW);
        response.setWindowState(WindowState.NORMAL);
        return null;
    }

    public String getOkLabel() {
        String resource = getResource(DsvanbangopyBundle.OK_LABEL);
        return resource;
    }

    private String getResource(String resourceKey) {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletConfig config =
            (PortletConfig)request.getAttribute("javax.portlet.config");
        return config.getResourceBundle(request.getLocale()).getString(resourceKey);
    }

    private String UrlChitietvanban() {
        String urlChitiet = "";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("Urlchitietvb") != null)
                urlChitiet = request.getParameter("Urlchitietvb").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlChitiet;
    }

    private String TieudeDsvanban() {
        String tieudeDsvanban = "Danh sách văn bản góp ý";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("tieudedsvbgopy") != null)
                tieudeDsvanban =
                        request.getParameter("tieudedsvbgopy").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return tieudeDsvanban;
    }

    public void setPageChitietvanban(String pageChitietvanban) {
        this.pageChitietvanban = pageChitietvanban;
    }

    public String getPageChitietvanban() {
        pageChitietvanban = UrlChitietvanban();
        return pageChitietvanban;
    }

    public void setTieudedsvbgopy(String tieudedsvbgopy) {
        this.tieudedsvbgopy = tieudedsvbgopy;
    }

    public String getTieudedsvbgopy() {
        tieudedsvbgopy = TieudeDsvanban();
        return tieudedsvbgopy;
    }

    private JUCtrlRangeBinding getDsVanbanView() {
        return Utils.getViewObject("DtvbDsVanbanGopyView1");
    }

    public String goLastPage() {
        Utils.goLastPage(RANGE_SIZE, "DtvbDsVanbanGopyView1");
        return null;
    }

    public boolean isLastEnabled() {
        return Utils.isLastEnabled(RANGE_SIZE, "DtvbDsVanbanGopyView1");
    }

    public boolean isFirstEnabled() {
        return Utils.isFirstEnabled(RANGE_SIZE, "DtvbDsVanbanGopyView1");
    }

    public void setPageNumber(int pageNumber) {
        JUCtrlRangeBinding dsvanbanView = this.getDsVanbanView();
        if (pageNumber >
            Math.max(1, (dsvanbanView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumber =
                    (int)Math.max(1, (dsvanbanView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumber = pageNumber;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPaging(boolean paging) {
        this.paging = paging;
    }

    public boolean isPaging() {
        return paging;
    }

    public String getLabelTrangdau() {
        labelTrangdau = Utils.getResource("TRANG_DAU");
        return labelTrangdau;
    }

    public String getLabelTrangcuoi() {
        labelTrangcuoi = Utils.getResource("TRANG_CUOI");
        return labelTrangcuoi;
    }

    public String getLabelTrangtruoc() {
        labelTrangtruoc = Utils.getResource("TRANG_TRUOC");
        return labelTrangtruoc;
    }

    public String getLabelTrangsau() {
        labelTrangsau = Utils.getResource("TRANG_SAU");
        return labelTrangsau;
    }

    public String getLabelNoData() {
        labelNoData = Utils.getResource("NO_DATA");
        return labelNoData;
    }

    public String getLabelAccess() {
        labelAccess = Utils.getResource("ACCESS_DENIED");
        return labelAccess;
    }

    public String getLangid() {
        langid = Utils.getPortalLocale().getLanguage();
        return langid;
    }
}
